/**
 * Filename: GAMobageSocialHandler.js
 * Author: Harris Khurram
 * Updates: Abid Hussain
 * Date Created: 05-12-2011
 * Date Updated: 05-18-2011
 */
 
/*
	fieldsOptions [ 
					"id", //Unique id - number
					"displayName", //string
					"aboutMe", //string
					"addresses", //plural field, has office and res address - not supported in gapi v1.0
					"age",	//number
					"birthday", //Date
					"gender",//string
					"hasApp", //bool
					"interests", //plural field - not supported in gapi v1.0
					"nickname",	//string -- beware this can be changed, do not bind things on nicknames, always use id
					"thumbnailUrl", //string
					"jobType", //string
					"bloodType", //string
					"ageRestricted" //bool
					]
*/

exports.GAMobageSocialHandler = Core.Class.singleton({
	classname: 'GAMobageSocialHandler',
	
	_fields: ["id", "displayName", "thumbnailUrl", "nickname"],
	_loggedInMobageUser: null,
	_loginRetryCount: 9,
	
	initialize: function()
	{
		this.loadCurrentMobageUser();
	},
	
	loadCurrentMobageUser: function() {
		this.loadCurrentMobageUserWithCallback(null);
	},
	
	loadCurrentMobageUserWithCallback: function(aCallback) {
		if(aCallback)
		{
			this._socialApiCall(this._fields, this._loadUserCallback.bind(this,aCallback));
		} else {
			this._socialApiCall(this._fields, this._loadUserCallback.bind(this));
		}
	},
	
	getCurrentMobageUser: function() {
		if(this._loggedInMobageUser != null) {
			return this._loggedInMobageUser;
		}
		else{
			this._socialApiCall(this._fields, this.getCurrentMobageUser.bind(this));
		}
	},
	
	isValidUser: function(){
		var returnAble = false;
		
		if(this._loggedInMobageUser == null){
			returnAble = false;
		} else {
			var oldUser = this._loggedInMobageUser;
			var uFields = ["id"];

			this.loadCurrentMobageUser();
			
			if(oldUser.id == this._loggedInMobageUser.id)
			{
				returnAble = true;
			}
		}
		
		return returnAble;
	},

	_socialApiCall: function(fields, callback) {
		
		UIFactory.showProgressHUD(GALocalizationHandler.getLocalizedString("contactingMobage"));
		if(!callback)
		{
			callback = function() { UIFactory.hideProgressHUD(); };
		}
		
		Social.Common.People.getCurrentUser(fields, function() { callback(), UIFactory.hideProgressHUD()});
	},
	
	_loadUserCallback: function(error, user,aCallback) {
		if (error) {
			if (this._loginRetryCount > 0) {
				this.loadCurrentMobageUser(aCallback);
				this._loginRetryCount = this._loginRetryCount - 1;
			} else {
				this._loginRetryCount = 9;
				UIFactory.hideProgressHUD();
				UIFactory.showToast(GALocalizationHandler.getLocalizedString("UserAuthorizationFailureMsg"));
			}
		} else {
		
			UIFactory.hideProgressHUD();
			
			this._loggedInMobageUser = user;
			if(aCallback != null){
				aCallback(this._loggedInMobageUser);
			}
		}
	},
	showCommunityButton: function(btnGravity,cbFunc){
		UIFactory.showProgressHUD(GALocalizationHandler.getLocalizedString("Contacting Mobage..."));
		Social.Common.Service.showCommunityButton(btnGravity, 'default',function(error){
            if(cbFunc){
                cbFunc();
            }
			if(!error) {
				console.log('I have added the community Button');
			} else {
				for(x in error)
					console.log('There was an error adding the community button: ' + error + " , " + x + " ,  " + error[x]);
			}
            UIFactory.hideProgressHUD()
		});
	},
	hideCommunityButton: function(){
		Social.Common.Service.hideCommunityButton();
	},
	
	setCallback: function(aCallback){
		this.aCallback = aCallback;
	},
	removeCallback: function(){
		this.aCallback = null;
	}
});